package com.project.systemfront.controller;


import com.project.systemfront.common.Result;
import com.project.systemfront.pojo.Artwork;
import com.project.systemfront.pojo.Likes;
import com.project.systemfront.pojo.dto.ArtDto;
import com.project.systemfront.pojo.vo.ArtworkVo;
import com.project.systemfront.service.IArtworkService;
import com.project.systemfront.service.ILikesService;
import com.project.systemfront.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/system/artwork")
public class ArtworkController
{
   @Autowired
   private IArtworkService artworkService;
   @Autowired
   private ILikesService likesService;

   @GetMapping
   public Result<List<Artwork>> getList(@RequestParam(required = false) Long categoryId, @RequestParam(required = false) String title)
   {
      Artwork artwork = new Artwork();
      System.out.println(categoryId);
      if(categoryId != null){
         artwork.setCategoryId(categoryId);
      }
      if(title != null){
         artwork.setTitle(title);
      }
      artwork.setStatus(1L);
      return Result.success(artworkService.selectArtworkList(artwork));
   }

   @GetMapping("/{id}")
   public Result<ArtworkVo> getInfo(@PathVariable("id") Long id)
   {
      ArtworkVo artwork = artworkService.selectArtworkById(id);
      return Result.success(artwork);
   }

  @GetMapping("/{id}/like")
   public Result<String> like(@PathVariable("id") Long id)
   {
      Map<String, Object> claims= ThreadLocalUtil.get();
      Long userId=Long.parseLong(claims.get("id").toString());
      artworkService.incrementLikeCount(id);
      Likes likes = new Likes();
      likes.setUserId(userId);
      likes.setArtworkId(id);
      likes.setCreateTime(new Date());
      likesService.insertLikes(likes);
      return Result.success("点赞成功");
   }

   @PostMapping("/add")
   public Result<String> add(@RequestBody ArtDto artdto)
   {
      Map<String, Object> claims= ThreadLocalUtil.get();
      Long id=Long.parseLong(claims.get("id").toString());
      Artwork artwork = new Artwork();
      artwork.setUserId(id);
      artwork.setTitle(artdto.getTitle());
      artwork.setDescription(artdto.getDescription());
      artwork.setImgUrl(artdto.getImgUrl());
      artwork.setStatus(1L);
      artwork.setCategoryId(artdto.getCategoryId());
      artwork.setCreateTime(new java.util.Date());
      artwork.setUpdateTime(new java.util.Date());
      artwork.setLikesCount(0L);
      artwork.setCommentsCount(0L);
      artwork.setFavoritesCount(0L);

      artworkService.insertArtwork(artwork);
      return Result.success("添加成功");
   }


   }
